/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_opengl.h"

#include "main.h"
#include "gfx.h"
#include "font_handler.h"
#include "menu.h"
#include "game_const.h"

const char *tiptext = "PRESS (UP) AND (DOWN) TO SELECT";
const char *tiptext2 = "PRESS (ENTER) TO BEGIN";

extern std::string level_passwords[];
extern int level_rules[];
extern Mix_Music *g_MainMusic;

int g_Options = 0;

#define DEFAULT_RULES GR_STATIC_DEFENCES|GR_LOW_ENEMY_COUNT|GR_4_COLORS|GR_PRIMES_MAX_20;

int g_CustomGameRules = DEFAULT_RULES;
int g_ContinueGame = -1;

std::string custom_game_options[12] =
{
	"PRIME SHOOTER MODE",
	"COLOR SHOOTER MODE",
	"DEFENCES TO PLACE",
	"MISSILES BOUNCE ON TOP",
	"MISSILES BOUNCE ON ENEMIES",
	"ENEMY COUNT",
	"TOUGH ENEMIES",
	"FAST ENEMIES",
	"FAST SHOOTING",
	"NUMBER OF COLORS",
	"MAX NUMBER",
	"ENEMIES DONT ATTACK"
};

std::string option_names[2] = 
{
	"USE COMPACT KEYMAP",
	"REMOVE ALL SPRITES"
};

MainMenu::MainMenu()
{
	m_menu = MENU_MAIN;
	m_finished = false;

	m_mainMenu_selected = -1;
	m_newgameMenu_selected = 0;
	m_newgameMenu_customEnabled = false;
	m_lastKeystroke = SDL_GetTicks();
	m_scrToLoad = SCREEN_NONE;

	m_animation = ANIMATION_FADEIN;
	m_animationTimer.start();

	Mix_PlayMusic(g_MainMusic, 0);
}

void MainMenu::render()
{
	bool selected = (m_mainMenu_selected != -1);
	glDrawTexuredRect( 0, 0, SCREEN_W, SCREEN_H, g_Textures[8] );

	if ( selected )
	{
		glScreenBlend();
		glDrawTexuredRect( 0, 0, SCREEN_W, SCREEN_H, g_Textures[9 + m_mainMenu_selected] );
		glAlphaBlend();
	}
	
	if ( m_animation != ANIMATION_CHANGE && selected )
	{
		renderMenuRow( m_mainMenu_selected, 0 );
	}
	else if ( m_animation == ANIMATION_CHANGE )
	{
		float progress = (float)m_animationTimer.get_ticks()/(float)ANIMATION_CHANGE_TIME;
		if ( progress > 1.0f )
		{
			m_animationTimer.stop();
			m_animation = ANIMATION_NONE;
			progress = 1.0f;
		}
		if ( m_lastMenu != -1 )
			renderMenuRow( m_lastMenu, (int)(progress*200) );
		renderMenuRow( m_mainMenu_selected, (int)((1.0f-progress)*-500) );
	}

	SDL_Rect r;
	if ( selected )
	{
		g_Fonts->get(FONT_TEXT)->textSize( tiptext, &r );
		glPrint(FONT_TEXT, (SCREEN_W/2) - (r.w/2), SCREEN_H - 15 - r.h, tiptext );
	}
	else if ( (SDL_GetTicks()/500)%4 < 3 && m_animation != ANIMATION_FADEIN )
	{
		g_Fonts->get(FONT_TEXT)->textSize( tiptext2, &r );
		glPrint(FONT_TEXT, (SCREEN_W/2) - (r.w/2), SCREEN_H - 15 - r.h, tiptext2 );
	}

	glDrawRect(0, 0, 8, SCREEN_H, 1, 1, 1);
	glDrawRect(0, 0, SCREEN_W, 8, 1, 1, 1);
	glDrawRect(SCREEN_W-8, 0, 8, SCREEN_H, 1, 1, 1);
	glDrawRect(0, SCREEN_H-8, SCREEN_W, 8, 1, 1, 1);
}

void MainMenu::renderMenuRow( int row, int x_offset )
{
	if ( row == -1 )
		return;

	glLoadIdentity();
	glTranslatef( 375.0f + x_offset, 15.0f, 0 );
	
	glBindTexture(GL_TEXTURE_2D, g_Textures[13]);
	glColor4f( 1, 1, 1, 1.0 );

	float h = 1.0f/(float)MENU_GFX_ROWS;

	switch(row)
	{
	case 0: row = 3; break;
	case 1: row = 1; break;
	case 2: row = 0; break;
	case 3: row = 2; break;
	}
	
	glBegin( GL_QUADS );
		glTexCoord2f( 0.0f, h*(row+1) ); glVertex3f( 0.0f,	0.0f,	0.0f );
		glTexCoord2f( 1.0f, h*(row+1) ); glVertex3f( 256.0f, 0.0f,	0.0f );
		glTexCoord2f( 1.0f, h*(row) ); glVertex3f( 256.0f,	64.0f,0.0f );
		glTexCoord2f( 0.0f, h*(row) ); glVertex3f( 0.0f,	64.0f,0.0f );
	glEnd();
	
	glBindTexture (GL_TEXTURE_2D, 0);
}

bool MainMenu::renderScrollingMenu( int &x, int &y, int &w, int &h )
{
	x = 230; 
	y = 95; 
	w = 385;
	h = 355;
	
	if ( m_animation == ANIMATION_SCROLLIN || m_animation == ANIMATION_SCROLLOUT)
	{
		float progress = (float)m_animationTimer.get_ticks()/(float)ANIMATION_SCROLL_TIME;
		if ( progress > 1.0f )
		{
			if ( m_animation == ANIMATION_SCROLLOUT )
			{
				m_menu = MENU_MAIN;
				m_animationTimer.stop();
				m_animation = ANIMATION_NONE;
				return false;
			}

			m_animationTimer.stop();
			m_animation = ANIMATION_NONE;
		}
		else
		{
			if ( m_animation == ANIMATION_SCROLLIN )
				x += (int)((1.0f-progress)*512);
			else x += (int)((progress)*512);
		}
	}

	glDrawRect( x, y, w, h, 0, 0, 0, 0.60f );
	
	glDrawRect(x, y, 8, h, 1, 1, 1);
	glDrawRect(x, y, w, 8, 1, 1, 1);
	glDrawRect(x+w-8, y, 8, h, 1, 1, 1);
	glDrawRect(x, y+h-8, w, 8, 1, 1, 1);
	
	return true;
}

void MainMenu::render_newgame()
{
	int x, y, w, h;
	if ( !renderScrollingMenu(x, y, w, h) )
		return;
	
	glPrintf( FONT_TEXT, x + 32, y + 16, "START NEW GAME (STORY MODE)" );
	glPrintf( FONT_TEXT, x + 32, y + 32, "PLAY CUSTOM LEVEL" );

	if (m_newgameMenu_customEnabled)
	{
		glDrawRect(x, y+32+16, w, 4, 1, 1, 1);
		glPrintf( FONT_TEXT, x + 16, y + 32, "*" );
		glPrintf( FONT_TEXT, x + 32, y + 64 + 13*16, "PLAY!" );
		if ( m_newgameMenu_selected == 12 )
			glPrintf( FONT_TEXT, x + 16, y + 64 + 13*16, "*" );
		else
			glPrintf( FONT_TEXT, x + 16, y + 64 + m_newgameMenu_selected*16, "*" );

		for ( int i=0; i<12; ++i )
		{
			glPrintf( FONT_TEXT, x+32, y + 64 + i*16, custom_game_options[i].c_str() );
			glPrintf( FONT_TEXT, x+256, y + 64 + i*16, getCustomGameRuleString(i) );
		}
	}
	else
	{
		glPrintf( FONT_TEXT, x + 16, y + 16 + m_newgameMenu_selected*16, "*" );
	}
}

void MainMenu::render_cfg()
{
	int x, y, w, h;
	if ( !renderScrollingMenu(x, y, w, h) )
		return;
		
	glPrintf( FONT_ALIEN, x + 32, y + 16, "GENERAL GAME OPTIONS" );
	
	glDrawRect(x, y+32+16, w, 4, 1, 1, 1);

	glPrintf( FONT_TEXT, x + 16, y + 64 + m_cfgMenu_selected*16, "*" );

	for ( int i=0; i<2; ++i )
	{
		glPrintf( FONT_TEXT, x+32, y + 64 + i*16, option_names[i].c_str() );
		glPrintf( FONT_TEXT, x+256, y + 64 + i*16, getOptionString(i) );
	}
}

const char *MainMenu::getOptionString( int option )
{
	return (g_Options & (1<<option))?"ENABLED":"DISABLED";
}

void MainMenu::render_load()
{
	int x, y, w, h;
	if ( !renderScrollingMenu(x, y, w, h) )
		return;
		
	glPrintf( FONT_ALIEN, x+32, y + 16, "ENTER LEVEL PASSWORD" );
	glPrintf( FONT_HEADER, x+90, y + 64, m_levelPass );
	glDrawRect(x+90+m_editPass*50, y + 120, 50, 8, 1, 1, 1);
	
	if ( m_continueTextTimer.get_ticks() > 0 )
	{
		if ( m_continueTextTimer.get_ticks() > 2000 )
		{
			m_continueTextTimer.stop();
		}
		else
		{
			SDL_Rect r;
			g_Fonts->get(FONT_TEXT)->textSize( m_continueText, &r );
			glPrint(FONT_TEXT, x + w/2 - r.w/2, y + 200, m_continueText);
		}
	}
}

void MainMenu::changeCustomGameRule( int rule )
{
	switch ( rule )
	{
	case 0:
		m_customGameRules ^= GR_PRIMESHOOTER;
		break;
	case 1: 
		m_customGameRules ^= GR_COLORSHOOTER;
		break;
	case 2: 
		if (m_customGameRules & GR_PLACE_DEFENCES_2)
		{
			m_customGameRules ^= GR_PLACE_DEFENCES_2;
			m_customGameRules ^= GR_PLACE_DEFENCES_4;
		}
		else if (m_customGameRules & GR_PLACE_DEFENCES_4)
		{
			m_customGameRules ^= GR_PLACE_DEFENCES_4;
			m_customGameRules ^= GR_STATIC_DEFENCES;
		}
		else if (m_customGameRules & GR_STATIC_DEFENCES)
		{
			m_customGameRules ^= GR_STATIC_DEFENCES;
			m_customGameRules ^= GR_PLACE_DEFENCES_2;
		}
		break;
	case 3: 
		m_customGameRules ^= GR_MISSILES_BOUNCE_ON_TOP;
		break;
	case 4: 
		m_customGameRules ^= GR_MISSILES_BOUNCE_ON_ENEMIES;
		break;
	case 5: 
		if (m_customGameRules & GR_LOW_ENEMY_COUNT)
		{
			m_customGameRules ^= GR_LOW_ENEMY_COUNT;
			m_customGameRules ^= GR_MED_ENEMY_COUNT;
		}
		else if (m_customGameRules & GR_MED_ENEMY_COUNT)
		{
			m_customGameRules ^= GR_MED_ENEMY_COUNT;
			m_customGameRules ^= GR_HIGH_ENEMY_COUNT;
		}
		else if (m_customGameRules & GR_HIGH_ENEMY_COUNT)
		{
			m_customGameRules ^= GR_HIGH_ENEMY_COUNT;
			m_customGameRules ^=  GR_LOW_ENEMY_COUNT;
		}
		break;
	case 6:
		m_customGameRules ^= GR_TOUGH_ENEMIES;
		break;
	case 7:
		m_customGameRules ^= GR_FAST_ENEMIES;
		break;
	case 8:
		m_customGameRules ^= GR_FAST_SHOOTING;
		break;
	case 9: 
		if (m_customGameRules & GR_4_COLORS)
		{
			m_customGameRules ^= GR_4_COLORS;
			m_customGameRules ^=  GR_6_COLORS;
		}
		else if (m_customGameRules & GR_6_COLORS)
		{		
			m_customGameRules ^= GR_6_COLORS;
			m_customGameRules ^= GR_8_COLORS;
		}
		else if (m_customGameRules & GR_8_COLORS)
		{
			m_customGameRules ^= GR_8_COLORS;
			m_customGameRules ^= GR_4_COLORS;
		}
		break;
	case 10: 
		if (m_customGameRules & GR_PRIMES_MAX_20)
		{
			m_customGameRules ^= GR_PRIMES_MAX_20;
			m_customGameRules ^= GR_PRIMES_MAX_100;
		}
		else if (m_customGameRules & GR_PRIMES_MAX_100)
		{
			m_customGameRules ^= GR_PRIMES_MAX_100;
			m_customGameRules ^= GR_PRIMES_MAX_500;
		}
		else if (m_customGameRules & GR_PRIMES_MAX_500)
		{
			m_customGameRules ^= GR_PRIMES_MAX_500;
			m_customGameRules ^= GR_PRIMES_MAX_1000;
		}
		else if (m_customGameRules & GR_PRIMES_MAX_1000)
		{
			m_customGameRules ^= GR_PRIMES_MAX_1000;
			m_customGameRules ^= GR_PRIMES_MAX_20;
		}
		break;
	case 11:
		m_customGameRules ^= GR_ENEMIES_DONT_ATTACK;
		break;
	}
}

const char *MainMenu::getCustomGameRuleString( int rule )
{
	switch ( rule )
	{
	case 0: 
		return (m_customGameRules & GR_PRIMESHOOTER)?"ENABLED":"DISABLED";
	case 1: 
		return (m_customGameRules & GR_COLORSHOOTER)?"ENABLED":"DISABLED";
	case 2: 
		if (m_customGameRules & GR_PLACE_DEFENCES_2)
			return "2 BARRIERS";
		else if (m_customGameRules & GR_PLACE_DEFENCES_4)
			return "4 BARRIERS";
		else if (m_customGameRules & GR_STATIC_DEFENCES)
			return "STATIC DEFENCE";
		else
			return "NO DEFENCE";
	case 3: 
		return (m_customGameRules & GR_MISSILES_BOUNCE_ON_TOP )?"ENABLED":"DISABLED";
	case 4: 
		return (m_customGameRules & GR_MISSILES_BOUNCE_ON_ENEMIES)?"ENABLED":"DISABLED";
	case 5: 
		if (m_customGameRules & GR_LOW_ENEMY_COUNT)
			return "LOW";
		else if (m_customGameRules & GR_MED_ENEMY_COUNT)
			return "MED";
		else if (m_customGameRules & GR_HIGH_ENEMY_COUNT)
			return "HIGH";
		else return "OFF";
	case 6: 
		return (m_customGameRules & GR_TOUGH_ENEMIES)?"ENABLED":"DISABLED";
	case 7: 
		return (m_customGameRules & GR_FAST_ENEMIES)?"ENABLED":"DISABLED";
	case 8: 
		return (m_customGameRules & GR_FAST_SHOOTING)?"ENABLED":"DISABLED";
	case 9: 
		if (m_customGameRules & GR_4_COLORS)
			return "4 COLORS";
		else if (m_customGameRules & GR_6_COLORS)
			return "6 COLORS";
		else if (m_customGameRules & GR_8_COLORS)
			return "ALL COLORS";
		else return "OFF";
	case 10: 
		if (m_customGameRules & GR_PRIMES_MAX_20)
			return "UP TO 20";
		else if (m_customGameRules & GR_PRIMES_MAX_100)
			return "UP TO 100";
		else if (m_customGameRules & GR_PRIMES_MAX_500)
			return "UP TO 500";
		else if (m_customGameRules & GR_PRIMES_MAX_1000)
			return "UP TO 1000";
	case 11: 
		return (m_customGameRules & GR_ENEMIES_DONT_ATTACK)?"ENABLED":"DISABLED";
	}

	return NULL;
}

void MainMenu::changeMenuSelection( int mov )
{
	if ( mov == 0 )
	{
		m_mainMenu_selected = 0;
		m_lastMenu = -1;
		Mix_PlayChannel( -1, g_Sounds[SOUND_MENU2], 1 );
	}
	else
	{
		m_lastMenu = m_mainMenu_selected;
		m_mainMenu_selected += mov;

		if ( m_mainMenu_selected < 0 )
					m_mainMenu_selected = MENU_GFX_ROWS - 1;
		else if ( m_mainMenu_selected > MENU_GFX_ROWS - 1  )
					m_mainMenu_selected = 0;
		Mix_PlayChannel( -1, g_Sounds[SOUND_MENU1], 0 );
	}

	m_lastKeystroke = SDL_GetTicks();

	m_animation = ANIMATION_CHANGE;
	m_animationTimer.start();
}

void MainMenu::onContinueKeypress( int key )
{
	switch ( key )
	{
	case KSHOOT:
		if ( strcmp( m_levelPass, "CHUC" ) == 0 )
		{
			g_CustomGameRules |= GR_GODMODE;
			for ( int i=0; i<16; ++i )
				level_rules[i] |= GR_GODMODE;
			m_continueTextTimer.start();
			strcpy( m_continueText, "Missiles dont kill aliens. Chuck Norris kills aliens." );
			strcpy(m_levelPass, "AAAA");
			m_editPass = 0;
		}
		else
		{
			for ( int i=0; i<16; ++i )
			{
				if ( level_passwords[i] == m_levelPass )
				{
					m_scrToLoad = SCREEN_GAME_CONTINUE;
					m_animation = ANIMATION_FADEOUT;
					g_CustomGameRules = m_customGameRules;
					g_ContinueGame = i;
					m_animationTimer.start();
					return;
				}
			}
		
			m_continueTextTimer.start();
			strcpy( m_continueText, "Jeez. Trying out passwords randomly?" );
			strcpy(m_levelPass, "AAAA");
			m_editPass = 0;
		}
		break;
	
	case KUP:
		m_levelPass[m_editPass]++;
		if ( m_levelPass[m_editPass] > 'Z' )
			 m_levelPass[m_editPass] = 'A';	
		break;
		
	case KDOWN:
		m_levelPass[m_editPass]--;
		if ( m_levelPass[m_editPass] < 'A' )
			 m_levelPass[m_editPass] = 'Z';
		break;
	
	case KLEFT:
		m_editPass--;
		if ( m_editPass < 0 )
			m_editPass = 3;
		break;
		
	case KRIGHT:
		m_editPass++;
		if ( m_editPass > 3 )
			m_editPass = 0;
		break;
	}
	
	m_lastKeystroke = SDL_GetTicks();
}

void MainMenu::onCfgKeypress( int key )
{
	if ( key == 0 )
	{
		g_Options ^= (1<<m_cfgMenu_selected);
	}
	else if ( key == -1 )
	{
		m_cfgMenu_selected--;
		if ( m_cfgMenu_selected < 0 )
		{
			m_cfgMenu_selected = 1;
		}
	}
	else if ( key == 1 )
	{
		m_cfgMenu_selected++;
		if ( m_cfgMenu_selected > 1 )
		{
			m_cfgMenu_selected = 0;
		}
	}
	
	m_lastKeystroke = SDL_GetTicks();
}

void MainMenu::onNewGameKeypress( int key )
{
	if ( m_newgameMenu_customEnabled )
	{
		if ( key == 0 )
		{
			if ( m_newgameMenu_selected == 12 )
			{
				m_scrToLoad = SCREEN_GAME_CUSTOM;
				m_animation = ANIMATION_FADEOUT;
				g_CustomGameRules = m_customGameRules;
				m_animationTimer.start();
			}
			else
			{
				changeCustomGameRule( m_newgameMenu_selected );
			}
		}
		else if ( key == -1 )
		{
			m_newgameMenu_selected--;
			if ( m_newgameMenu_selected < 0 )
			{
				m_newgameMenu_selected = 12;
			}
		}
		else if ( key == 1 )
		{
			m_newgameMenu_selected++;
			if ( m_newgameMenu_selected > 12 )
			{
				m_newgameMenu_selected = 0;
			}
		}
	}
	else
	{
		if ( key == 0 )
		{
			if ( m_newgameMenu_selected == 1 )
			{
				m_newgameMenu_customEnabled = true;
				m_newgameMenu_selected = 0;
				m_customGameRules = DEFAULT_RULES;
			}
			else
			{
				// fade out to new game.
				m_scrToLoad = SCREEN_GAME_NEW;
				m_animation = ANIMATION_FADEOUT;
				m_animationTimer.start();
			}
		}
		else if ( key == -1 || key == 1 )
		{
			m_newgameMenu_selected = !m_newgameMenu_selected;
		}
	}

	m_lastKeystroke = SDL_GetTicks();
}

void MainMenu::moveToMenuScreen( int scr )
{
	if ( scr == -1 )
	{
		Mix_PlayChannel( -1, g_Sounds[SOUND_PLAYERDEAD], 0 );
		if ( m_menu == MENU_MAIN )
		{
			exit(0);
		}
		else if ( m_menu == MENU_NEWGAME )
		{
			if ( m_newgameMenu_customEnabled )
			{
				m_newgameMenu_customEnabled = false;
				m_newgameMenu_selected = 1;
			}
			else
			{
				m_animation = ANIMATION_SCROLLOUT;
				m_animationTimer.start();
			}
		}
		else if ( m_menu == MENU_CONTINUE || m_menu == MENU_CONFIGURE )
		{
			m_animation = ANIMATION_SCROLLOUT;
			m_animationTimer.start();
		}
	}
	else if ( scr == 0 )
	{
		Mix_PlayChannel( -1, g_Sounds[SOUND_MENU2], 0 );
		if ( m_menu == MENU_MAIN )
		{
			switch( m_mainMenu_selected )
			{
			case 0:
				m_menu = MENU_NEWGAME;
				m_animation = ANIMATION_SCROLLIN;
				m_animationTimer.start();
				break;

			case 1:
				m_cfgMenu_selected = 0;
				m_menu = MENU_CONFIGURE;
				m_animation = ANIMATION_SCROLLIN;
				m_animationTimer.start();
				break;
			case 2:
				exit(0);

			case 3:
				strcpy(m_levelPass, "AAAA");
				m_editPass = 0;
				m_menu = MENU_CONTINUE;
				m_animation = ANIMATION_SCROLLIN;
				m_animationTimer.start();
				break;	
			}
		}
		else 
		{

		}
	}
	else
	{
		m_menu = scr;
	}
	m_lastKeystroke = SDL_GetTicks();
}

void MainMenu::tick()
{
	if ( m_finished )
	{
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		return;
	}
	
	if ( m_animation == ANIMATION_NONE )
	{
		if ( m_lastKeystroke + 200 < SDL_GetTicks() )
		{
			switch( m_menu )
			{
			case MENU_MAIN:
				if( m_mainMenu_selected != -1 )
				{
					if ( g_Keys[KUP] )
					{
						changeMenuSelection(-1);
					}	
					else if ( g_Keys[KDOWN] )
					{
						changeMenuSelection(1);
					}
					else if ( g_Keys[KSHOOT] )
					{
						moveToMenuScreen( 0 );
					}
					else if ( g_Keys[KSCAPE] )
					{
						moveToMenuScreen( -1 );
					}
				}
				else
				{
					if ( g_Keys[KSHOOT] )
					{
						changeMenuSelection(0);
					}
					else if ( g_Keys[KSCAPE] )
					{
						moveToMenuScreen( -1 );
					}
				}
				break;
				
			case MENU_NEWGAME:
				if ( g_Keys[KUP] )
				{
					onNewGameKeypress(-1);
				}	
				else if ( g_Keys[KDOWN] )
				{
					onNewGameKeypress(1);
				}
				else if ( g_Keys[KSHOOT] )
				{
					onNewGameKeypress( 0 );
				}
				else if ( g_Keys[KSCAPE] )
				{
					moveToMenuScreen( -1 );
				}
				break;
				
			case MENU_CONTINUE:
				if ( g_Keys[KUP] )
				{
					onContinueKeypress(KUP);
				}	
				else if ( g_Keys[KDOWN] )
				{
					onContinueKeypress(KDOWN);
				}
				else if ( g_Keys[KSHOOT] )
				{
					onContinueKeypress(KSHOOT);
				}
				else if ( g_Keys[KLEFT] )
				{
					onContinueKeypress(KLEFT);
				}
				else if ( g_Keys[KRIGHT] )
				{
					onContinueKeypress(KRIGHT);
				}
				else if ( g_Keys[KSCAPE] )
				{
					moveToMenuScreen( -1 );
				}
				break;
				
			case MENU_CONFIGURE:
				if ( g_Keys[KUP] )
				{
					onCfgKeypress(-1);
				}	
				else if ( g_Keys[KDOWN] )
				{
					onCfgKeypress(1);
				}
				else if ( g_Keys[KSHOOT] )
				{
					onCfgKeypress( 0 );
				}
				else if ( g_Keys[KSCAPE] )
				{
					moveToMenuScreen( -1 );
				}
				break;
			}
		}
	}
		
	glEnable2D();
	glEnable( GL_BLEND );

	render();
	
	switch ( m_menu )
	{
	case MENU_NEWGAME: render_newgame(); break;
	case MENU_CONTINUE: render_load(); break;
	case MENU_CONFIGURE: render_cfg(); break;
	}

	if ( m_animation == ANIMATION_FADEIN || m_animation == ANIMATION_FADEOUT)
	{
		float progress = (float)m_animationTimer.get_ticks()/(float)ANIMATION_FADE_TIME;
		if ( progress > 1.0f )
		{
			if ( m_animation == ANIMATION_FADEOUT )
			{
				if ( progress > 0.5 )
					m_finished = true;
			}
			else 
			{
				m_animationTimer.stop();
				m_animation = ANIMATION_NONE;
			}
		}
		else
		{
			if ( m_animation == ANIMATION_FADEOUT )
				glDrawRect( 0, 0, SCREEN_W, SCREEN_H, 0, 0, 0, progress );
			else
				glDrawRect( 0, 0, SCREEN_W, SCREEN_H, 0, 0, 0, 1.0f-progress );
		}
	}
	
	glDisable(GL_BLEND);
	glDisable2D();
}

void MainMenu::onEvent ( SDL_Event *event )
{

}
